package com.dw.etl.core.controller;

import javax.servlet.http.HttpServletRequest;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.core.bean.vo.LayMsg;
import org.springframework.beans.factory.annotation.Autowired;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.core.api.MdBaseDictService;
import com.dw.etl.core.bean.model.MdBaseDict;
import org.springframework.util.StringUtils;
import com.dw.etl.common.vo.PaginationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * mdBaseDict控制器
 * Created by hozeData on 2021/07/13.
 */
@RestController
@RequestMapping("/etl/mdBaseDict/")
@Api(description = "字典管理", tags = "字典管理")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdBaseDictController {

    @Autowired
    MdBaseDictService mdBaseDictService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findByPage", method = RequestMethod.GET)
    public LayMsg findByPage(Integer page, Integer limit, String filter) {
        MdBaseDict mdBaseDict = new MdBaseDict();
        mdBaseDict.setNa(filter);
        PaginationVO<MdBaseDict> pageVO = mdBaseDictService.findPageByModel(mdBaseDict, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value = "插入")
    @ApiImplicitParam(name = "mdBaseDict", value = "实体", paramType = "body", required = true, dataType = "MdBaseDict")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseResult insert(@RequestBody MdBaseDict mdBaseDict, HttpServletRequest request) {
        int num = mdBaseDictService.insertSelective(mdBaseDict);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "idDict", value = "主键", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public BaseResult getById(String idDict, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idDict)) {
            throw new BaseException("请选择要编辑的项");
        }
        MdBaseDict mdBaseDict = mdBaseDictService.getById(idDict);
        return new BaseResult(CommonResultConstant.SUCCESS, mdBaseDict);
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "mdBaseDict", value = "实体", paramType = "body", required = true, dataType = "MdBaseDict")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseResult update(@RequestBody MdBaseDict mdBaseDict, HttpServletRequest request) {
        int num = mdBaseDictService.updateByIdSelective(mdBaseDict);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "idDict", value = "主键", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(String idDict, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idDict)) {
            throw new BaseException("请选择要删除的项");
        }
        int num = mdBaseDictService.deleteById(idDict);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "mdBaseDict", value = "实体", paramType = "body", required = true, dataType = "MdBaseDict")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BaseResult save(@RequestBody MdBaseDict mdBaseDict, HttpServletRequest request) {
        int num;
        if (StringUtils.isEmpty(mdBaseDict.getIdDict())) {
            num = mdBaseDictService.insertSelective(mdBaseDict);
        } else {
            num = mdBaseDictService.updateById(mdBaseDict);
        }

        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }


}
